﻿package frEngine.animateControler.particleControler
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.animateControler.particleControler.animate.*;
    import frEngine.core.mesh.*;
    import frEngine.event.*;
    import frEngine.math.*;
    import frEngine.render.layer.*;
    import frEngine.shader.*;
    import frEngine.shader.registType.*;

    public class New_ParticleAnimateControler2 extends ParticleAnimateControler
    {
        private var globleRotaionValue:Quaternion;
        private var globleScaleValue:Vector.<Number>;
        private var vc0_100_reg:VcParam;
        private var _tempVect3d:Vector3D;
        private var _maxDuring:int = 0;
        private var _perMaxDuring:int = 0;
        private var _playMode:int = 0;
        private var hasUpload:Boolean;
        private var _transformMatrix3d:Matrix3D;
        private var _particleStatus:ParticleStatus;
        private var preDuring:int = -1;
        private var _toltalParticleNum:int = 0;
        private var _numOfMeshVertex:int = 1;
        private var _perParticleEmitterTime:Number;
        private var _emmitFinish:Boolean;
        private var _preFrame:int = -1;
        private var _preTime:int = 0;
        private var _useRotation:Boolean;
        private var _useScale:Boolean = true;
        private var timeOffsetValue:Vector.<Number>;
        private var endPoint:Vector3D;
        private var _curPlayTime:int = 0;

        public function New_ParticleAnimateControler2()
        {
            this.globleRotaionValue = new Quaternion();
            this.globleScaleValue = this.Vector.<Number>([1, 1, 1, -1]);
            this._tempVect3d = new Vector3D();
            this._particleStatus = new ParticleStatus();
            return;
        }// end function

        override public function set useScale(param1:Boolean) : void
        {
            this._useScale = param1;
            if (param1)
            {
                needUpdateTransform = true;
            }
            else
            {
                var _loc_2:int = 1;
                this.globleScaleValue[2] = 1;
                var _loc_2:* = _loc_2;
                this.globleScaleValue[1] = _loc_2;
                this.globleScaleValue[0] = _loc_2;
            }
            return;
        }// end function

        override public function set useRotation(param1:Boolean) : void
        {
            this._useRotation = param1;
            if (param1)
            {
                needUpdateTransform = true;
            }
            else
            {
                var _loc_2:int = 0;
                this.globleRotaionValue.z = 0;
                var _loc_2:* = _loc_2;
                this.globleRotaionValue.y = _loc_2;
                this.globleRotaionValue.x = _loc_2;
                this.globleRotaionValue.w = 1;
            }
            return;
        }// end function

        private function toUpdateParticleTransform() : void
        {
            var _loc_1:Vector.<Vector3D> = null;
            var _loc_2:Vector3D = null;
            needUpdateTransform = false;
            if (this._useRotation || this._useScale)
            {
                _loc_1 = targetMesh.world.decompose(Orientation3D.QUATERNION);
            }
            if (this._useRotation)
            {
                _loc_2 = _loc_1[1];
                this.globleRotaionValue.x = _loc_2.x;
                this.globleRotaionValue.y = _loc_2.y;
                this.globleRotaionValue.z = _loc_2.z;
                this.globleRotaionValue.w = _loc_2.w;
                if (_loc_2.w < 0)
                {
                    this.globleRotaionValue.x = this.globleRotaionValue.x * -1;
                    this.globleRotaionValue.y = this.globleRotaionValue.y * -1;
                    this.globleRotaionValue.z = this.globleRotaionValue.z * -1;
                    this.globleRotaionValue.w = this.globleRotaionValue.w * -1;
                }
            }
            if (this._useScale)
            {
                this._tempVect3d = _loc_1[2];
                this.globleScaleValue[0] = this._tempVect3d.x;
                this.globleScaleValue[1] = this._tempVect3d.y;
                this.globleScaleValue[2] = this._tempVect3d.z;
            }
            return;
        }// end function

        override public function set particleParams(param1:ParticleParams) : void
        {
            var _loc_2:Label3D = null;
            _particleParams = param1;
            if (!_particleParams || !param1.hasCompleted)
            {
                this._emmitFinish = false;
                return;
            }
            this._playMode = _particleParams.playMode;
            this._toltalParticleNum = _particleParams.total_number;
            this._numOfMeshVertex = _particleParams.numOfmeshVertex;
            this._perParticleEmitterTime = _particleParams.perParticleEmitterTime;
            useEndPoint = _particleParams.useEndPoint;
            this._maxDuring = _particleParams.maxDuring;
            if (!cuPlayLable)
            {
                _loc_2 = new Label3D(0, int((_particleParams.totalTime - 1)), "default", 0);
                this.setPlayLable(_loc_2);
            }
            else
            {
                this.cuPlayLable.change(0, (_particleParams.totalTime - 1));
            }
            this._particleStatus.reinit(_particleParams);
            this.toUpdateAnimate(true);
            return;
        }// end function

        override protected function setingParams(event:Event) : void
        {
            var _loc_6:FsParam = null;
            var _loc_7:uint = 0;
            var _loc_8:uint = 0;
            var _loc_9:Vector.<Number> = null;
            var _loc_10:Vector.<Number> = null;
            var _loc_11:XYZObject = null;
            var _loc_12:Number = NaN;
            var _loc_2:* = targetMesh.material;
            this.timeOffsetValue = _loc_2.getParam("{time_perEmitterTime_emitterLife}", true).value;
            this.timeOffsetValue[0] = 0;
            this.timeOffsetValue[1] = _particleParams.perParticleEmitterTime;
            this.timeOffsetValue[2] = _particleParams.emitterLife;
            if (_particleParams.useColor || _particleParams.useAlpha)
            {
                _loc_6 = _loc_2.getParam("{dateaTexture}", false);
                var _loc_13:* = ParticleMesh(targetMesh).particleFileData.dateaTexture;
                _loc_6.value = ParticleMesh(targetMesh).particleFileData.dateaTexture;
                _loc_7 = _particleParams.useColor ? (1) : (0);
                _loc_8 = _particleParams.useAlpha ? (1) : (0);
                _loc_9 = _loc_2.getParam("{useColorOrAlpha}", false).value;
                _loc_9[0] = _loc_7;
                _loc_9[1] = _loc_8;
            }
            _loc_2.getParam("{globalSize}", true).value = this.globleScaleValue;
            var _loc_3:* = _loc_2.getParam("{faceCameraConst}", true).value;
            var _loc_4:* = _particleParams.faceCamera ? (1) : (0);
            _loc_3[0] = _loc_4;
            _loc_3[1] = 1 - _loc_4;
            var _loc_5:* = _loc_2.getParam("{uvStep}", false);
            if (_loc_5)
            {
                _loc_10 = _loc_5.value;
                _loc_11 = _particleParams.uvFingerConst ? (_particleParams.uvFingerConst) : (_particleParams.uvStep);
                if (_loc_11)
                {
                    _loc_12 = isNaN(_loc_11.z) ? (1) : (_loc_11.z);
                    _loc_10[0] = 1 / _loc_11.x;
                    _loc_10[1] = 1 / _loc_11.y;
                }
                else
                {
                    _loc_10[0] = 1;
                    _loc_10[1] = 1;
                }
            }
            this.vc0_100_reg = _loc_2.getParam("{vc0-100}", true);
            needUpdateTransform = true;
            this.hasUpload = true;
            this.toUpdateAnimate(true);
            return;
        }// end function

        override public function play() : void
        {
            if (_isPlaying)
            {
                return;
            }
            super.play();
            return;
        }// end function

        override protected function updateCurFrame() : void
        {
            var _loc_1:Number = NaN;
            var _loc_3:Boolean = false;
            if (this._playMode == 1)
            {
                _loc_1 = targetMesh.timerContorler.totalframe;
            }
            else
            {
                _loc_1 = targetMesh.timerContorler.curFrame;
            }
            var _loc_2:* = cuPlayLable.length;
            if (_loc_1 >= _loc_2)
            {
                _loc_1 = _loc_2 - 1;
            }
            else if (_loc_1 < 0)
            {
                _loc_1 = 0;
            }
            this.currentFrame = cuPlayLable.from + _loc_1;
            return;
        }// end function

        override public function toUpdateAnimate(param1:Boolean = false) : void
        {
            var _loc_3:Number = NaN;
            if (!this.hasUpload || !_particleParams)
            {
                return;
            }
            super.toUpdateAnimate(param1);
            if (!param1 && !changeFrame)
            {
                return;
            }
            this._curPlayTime = this.currentFrame - this._curStarPlayTime;
            if (this._curPlayTime < 0)
            {
                this._curPlayTime = 0;
            }
            this.timeOffsetValue[0] = this._curPlayTime;
            var _loc_2:* = int(this._curPlayTime / _particleParams.emitterLife);
            this._emmitFinish = _loc_2 > this._maxDuring ? (true) : (false);
            if (this._emmitFinish)
            {
                return;
            }
            if (_particleParams.useScreenEmitter)
            {
                _loc_3 = 1 / Global3D.camera.zoom;
                this.globleScaleValue[0] = _loc_3;
                this.globleScaleValue[1] = _loc_3;
                this.globleScaleValue[2] = _loc_3;
            }
            if (useEndPoint && _useTargetEndPoint)
            {
                this.endPoint = ParticleMesh(targetMesh).hangObjPoint;
            }
            return;
        }// end function

        override public function draw(param1:Mesh3D, param2:ShaderBase = null) : void
        {
            if (!_particleParams || this._emmitFinish)
            {
                return;
            }
            if (!targetSurface || !targetMesh.material.hasPrepared(param1, targetSurface))
            {
                return;
            }
            if (needUpdateTransform)
            {
                targetObject3d.world.copyColumnTo(3, _globlePoint);
                this.toUpdateParticleTransform();
            }
            this._particleStatus.update(this._curPlayTime, _globlePoint, this.globleRotaionValue, this.endPoint, lineRoadControler);
            var _loc_4:* = Global3D;
            var _loc_5:* = Global3D.objectsDrawn + 1;
            _loc_4.objectsDrawn = _loc_5;
            var _loc_3:* = param1.material.materialParams;
            if (_loc_3.enableMask && param1.layer != Layer3DManager.warpLayer)
            {
                Global3D.scene.context.setStencilReferenceValue(0);
            }
            this._particleStatus.draw(this.vc0_100_reg, targetMesh, targetSurface);
            if (_loc_3.enableMask && param1.layer != Layer3DManager.warpLayer)
            {
                Global3D.scene.context.setStencilReferenceValue(1);
            }
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose();
            _particleParams = null;
            this._particleStatus.dispose();
            this._maxDuring = 0;
            this._perMaxDuring = 0;
            this._playMode = 0;
            this.hasUpload = false;
            this._transformMatrix3d = null;
            _emitterTime = 0;
            _curStarPlayTime = 0;
            this.preDuring = -1;
            this._toltalParticleNum = 0;
            this._numOfMeshVertex = 1;
            this._perParticleEmitterTime = 0;
            this._emmitFinish = false;
            this._preFrame = -1;
            this._preTime = 0;
            emmitFlag = 1;
            return;
        }// end function

    }
}
